/************************************  

DESCRIPTION: This file puts together the block estimates for women

Datasets used:
 (1) femHypBlocks
 (2) fembetaBlockHyp_lt20
 (3) fembetaBlockHyp_2034
 (4) fembetaBlockHyp_gt34
 (5) femDiabBlocks
 (6) fembetaBlockDiab
 (7) femAllBlocks
 (8) fembetaBlockDeath
 
Datasets created:
 (1) fembetaHyp_overall 
 (2) fembetaDiab_overall 
 (3) femblockDeath_overall 
 

************************************/  

set more off
capture log close
clear all

set matsize 10000

****************HYPERTENSION****************

*Block size
	tempfile blksize
	use ./femHypBlocks.dta
		 egen totTreat = sum(VMERGER)
		 egen blkTreat = sum(VMERGER), by(block)

		 keep block totTreat blkTreat
		 duplicates drop
		 gen shareblk= blkTreat/totTreat
		 sort block
	save `blksize', replace
	clear

*Combine diabetes results
	tempfile hyp1
	use  ./fembetaBlockHyp_lt20.dta
		capture drop *20
		gen id=_n
		sort id
	save `hyp1'
	clear

	tempfile hyp2
	use ./fembetaBlockHyp_2034.dta
		gen id=_n
		sort id
	save `hyp2'
	clear

	tempfile hyp3
	use ./fembetaBlockHyp_gt34.dta
		gen id=_n
		sort id
	save `hyp3'
	clear

	use `hyp1'
	merge 1:1 id using `hyp2'
	drop _merge
	merge 1:1 id using `hyp3'
	drop _merge

		rename rnacuteicd8_1 coefnames
		drop rn*
		sort coefnames

	*Keep only postmerger
	keep if coefnames =="1.postmerger"
		gen i=1
	reshape long rn sesq_ beta_, i(i) j(dis) str
	drop i rn
		gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
		 destring block, replace
		gen outcome = regexr(dis,"[\_][0-9]+","")

	sort block 

	merge m:1 block using `blksize'
	drop _merge

		*Treatment effect on the treated
		sort outcome block
		*Weights are treatment on the treated
		gen wbeta= beta_*shareblk
		gen wsesq= sesq_*shareblk
		*Treatment effect
		egen te = sum(wbeta), by(outcome)
		*Variance
		egen te_var = sum(wsesq), by(outcome)
		*SE
		gen te_se = sqrt(te_var) 

	save ./fembetaHyp_overall.dta, replace

****************Death****************

	*Block size
	tempfile blksize
	use ./femAllBlocks.dta
	 egen totTreat = sum(VMERGER)
	 egen blkTreat = sum(VMERGER), by(block)

	 keep block totTreat blkTreat
	 duplicates drop
	 gen shareblk= blkTreat/totTreat
	 sort block
	save `blksize', replace
	clear


	*Sort lower blocks
	use  ./fembetaBlockDeath.dta
		rename rndeath_2 coefnames
		sort coefnames

	*Keep only postmerger
	keep if rndeath_1 =="1.postmerger"
		gen i=1
	reshape long rn sesq_ beta_, i(i) j(dis) str
	drop i rn
		gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
		 destring block, replace
		gen outcome = regexr(dis,"[\_][0-9]+","")

		sort block 

	merge m:1 block using `blksize'
		drop _merge

		*Treatment effect on the treated
		sort outcome block
		*Weights are treatment on the treated
		gen wbeta= beta_*shareblk
		gen wsesq= sesq_*shareblk
		*Treatment effect
		egen te = sum(wbeta), by(outcome)
		*Variance
		egen te_var = sum(wsesq), by(outcome)
		*SE
		gen te_se = sqrt(te_var) 

	save ./femblockDeath_overall.dta, replace
	clear


****************DIABETES****************

	tempfile blksize
	use ./femDiabBlocks.dta
	 egen totTreat = sum(VMERGER)
	 egen blkTreat = sum(VMERGER), by(block)

	 keep block totTreat blkTreat
	 duplicates drop
	 gen shareblk= blkTreat/totTreat
	 sort block
	save `blksize', replace
	clear

	use ./fembetaBlockDiab.dta
		rename rndiabcomp1_1 coefnames
		sort coefnames
	drop if beta_diabcomp1_1==.

	*Keep only postmerger
	keep if coefnames =="1.postmerger"
		gen i=1
	reshape long rn sesq_ beta_, i(i) j(dis) str
	drop i rn
		gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
		 destring block, replace
		gen outcome = regexr(dis,"[\_][0-9]+","")

		sort block 

	merge m:1 block using `blksize'
	drop _merge

		*Treatment effect on the treated
		sort outcome block
		*Weights are treatment on the treated
		gen wbeta= beta_*shareblk
		gen wsesq= sesq_*shareblk
		*Treatment effect
		egen te = sum(wbeta), by(outcome)
		*Variance
		egen te_var = sum(wsesq), by(outcome)
		*SE
		gen te_se = sqrt(te_var) 

	save ./fembetaDiab_overall.dta, replace
